//
//  YFStatisticsApi.m
//  CoollangTennisBall
//
//  Created by Coollang on 2018/1/15.
//  Copyright © 2018年 Coollang-YF. All rights reserved.
//

#import "YFStatisticsApi.h"
#import "DeleteDate.h"

@implementation YFStatisticsApi

// 获取用户更新主页数据最新的时间戳
+ (instancetype)getUseruploadSportDataLastTimeWithComplete:(void(^)(BOOL success,NSString *lastTimeStamp))completeBlock {
    YFStatisticsApi *uploadTimeApi = [YFStatisticsApi requestApiWithType:kStatisticGetUserUploadTimeApi andParam:nil];
    [[YFNetworkingService sharedInstance] sendRequest:uploadTimeApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            if (completeBlock) {
                completeBlock(YES,[[result responseInfo] objectForKey:@"LastUploadTime"]);
            }
        }else {
            if (completeBlock) {
                completeBlock(NO,nil);
            }
        }
    }];
    return uploadTimeApi;
}

/**
 * 获取主页运动数据
 *
 * localLastUploadTime: 本地最新的时间戳
 */
+ (instancetype)getMainHomeSportDatasWithUploadTime:(NSString *)localLastUploadTime withComplete:(void(^)(BOOL success,YFStatisticsApi *resultApi, NSString *lastUploadTime,NSArray<SportRecordItem *> *sportRecordItemsList))completeBlock; {
    
    YFStatisticsApi *getHomeDatasApi = [YFStatisticsApi requestApiWithType:kStatisticGetGetHomeSportDataApi andParam:@{@"LastUploadTime":localLastUploadTime}];
    
    [[YFNetworkingService sharedInstance] sendRequest:getHomeDatasApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            NSArray *items = [getHomeDatasApi sportRecordItemsListWithDataArr:[[getHomeDatasApi responseInfo] objectForKey:@"Data"]];
            NSString *lastTimeStamp = [[getHomeDatasApi responseInfo] objectForKey:@"LastUploadTime"];
            if (completeBlock) {
                completeBlock(YES,getHomeDatasApi,lastTimeStamp,items);
            }
        }else {
            if (completeBlock) {
                completeBlock(NO,getHomeDatasApi,nil,nil);
            }
        }
    }];
    return getHomeDatasApi;
}
- (NSArray<SportRecordItem *> *)sportRecordItemsListWithDataArr:(NSArray *)jsonArr {
    NSMutableArray *itemArr = [NSMutableArray array];
    for (NSDictionary *dataDic in jsonArr) {
        SportRecordItem * item = [[SportRecordItem alloc] init];
        [item analysisItemWithDictionary:dataDic];
        [itemArr addObject:item];
    }
    return itemArr;
}

// 获取数据统计目标
+ (instancetype)getStatisticTarget {
    YFStatisticsApi *getTargetAPi = [YFStatisticsApi requestApiWithType:kStatisticGetTargetApi andParam:nil];
    return getTargetAPi;
}
// 修改数据统计目标
+ (instancetype)changeStatisticsTargetWithCount:(NSInteger)count {
    return [YFStatisticsApi requestApiWithType:kStatisticSaveTargetApi andParam:@{@"count":[NSString stringWithFormat:@"%ld",(long)count]}];
}

/**
 * 删除或恢复主页某一天运动数据
 *
 * dayString  //格式  2016-05-12
 * flag  //0表示恢复 1表示删除
 */
+ (instancetype)deleteHomeSportDatawithDateString:(NSString *)dayString flagReocrd:(BOOL)flag {
    return [YFStatisticsApi requestApiWithType:kStatisticDeleteSportDataApi andParam: @{ @"date": dayString,@"flag": (flag ? @"1" : @"0")}];
}

/**
 * 获取主页已删除的运动数据
 */
+ (instancetype)getHomeDeleteListWithComlpete:(void(^)(BOOL success,NSArray <DeleteDate *>*deleteSportDates))completeBlock {
    YFStatisticsApi *getDeletelistApi = [YFStatisticsApi requestApiWithType:kStatisticGetRecordDeleteListApi andParam:nil];
    
    [[YFNetworkingService sharedInstance] sendRequest:getDeletelistApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            NSMutableArray *deleteDates = [@[] mutableCopy];
            for (NSDictionary *deleteDateDictionary in [getDeletelistApi responseInfo]) {
                DeleteDate *deleteDate = [[DeleteDate alloc] initWithDeleteDateDictionary:deleteDateDictionary];
                [deleteDates addObject:deleteDate];
            }
            if (completeBlock) {
                completeBlock(YES,deleteDates.copy);
            }
        }else {
            if (completeBlock) {
                completeBlock(NO,nil);
            }
        }
    }];
    
    return getDeletelistApi;
}

+ (instancetype)getSportDetailReportWithSportDateString:(NSString *)dateString {
    // lang : 仅支持字符串 zh_cn  english france
    YFLanguage curentLanguage = [NSBundle currentLangeuage];
    NSString *langStr = @"english";
    if (curentLanguage == kLanguageChineseSimple) {
        langStr = @"zh_cn";
    }else if (curentLanguage == kLanguageFrennch) {
        langStr = @"france";
    }else {
        langStr = @"english";
    }
    NSDictionary *dict = @{
                           @"date": dateString,
                           @"lang": langStr
                           };
    return [YFStatisticsApi requestApiWithType:kStatisticGetSportDetailReportAPi andParam:dict];
}



/**
 * 上传主页运动数据
 *
 * localLastUploadTime: 本地最新的时间戳
 */
+ (instancetype)uploadHomeSportDataWithSportItems:(NSArray <SportRecordItem *>*)sportItems   {
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:[self performHandleData:sportItems] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *parameStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *parame = @{@"data":parameStr};
    
    return [YFStatisticsApi requestApiWithType:kStatisticUploadHomeSportDataApi andParam:parame];
}

+ (NSArray *)performHandleData:(NSArray *)sportItems {
    
    NSMutableArray *parames = [NSMutableArray array];
    
    for (SportRecordItem *item in sportItems) {
        NSMutableArray * barChartItems = [NSMutableArray array];
        for (BarChartItem *chartItem in item.barChartItems) {
            NSDictionary *tempDic = @{
                                      @"Speed": chartItem.maxSpeed,
                                      @"Force": chartItem.strength,
                                      @"Radian": chartItem.arc,
                                      @"Timestamp":chartItem.unixTimeStamp,
                                      @"Sweet": [@(chartItem.isHitSweet) stringValue],
                                      @"Type": [@(chartItem.poseType) stringValue],
                                      @"H": [@(chartItem.handType) stringValue]
                                      };
            [barChartItems addObject:tempDic];
        }
        
        NSDictionary *dic = @{
                              @"DateTimestamp": [@(item.dateTimeStamp) stringValue],
                              @"Date":item.dateStr,
                              @"Caroline": [@(item.calories) stringValue],
                              @"Duration": [@(item.exerciseTimeInterval) stringValue],
                              @"MaxSpeed": [@(item.maxSpeed) stringValue],
                              @"TypeCount": @{
                                      @"PingJi": [@(item.pingJiCount) stringValue],
                                      @"XuanQiu": [@(item.xuanQiuCount) stringValue],
                                      @"XueQiu": [@(item.xueQiuCount) stringValue],
                                      @"JieJi": [@(item.jieJiCount) stringValue],
                                      @"GaoYa": [@(item.kouShaCount) stringValue],
                                      @"FaQiu": [@(item.faQiuCount) stringValue]
                                      },
                              @"DetailData": barChartItems
                              };
        [parames addObject:dic];
    }
    return parames.copy;
}


@end
